/// 
/// Author: Jeff Liu
/// Date: 2021-12-31 15:04:15
/// LastEditTime: 2023-06-01 10:36:31
/// LastEditors: Jeff Liu
/// Description: 
/// FilePath: /HCCBJ/src/cls/HCC/SVR/Prod/BS/SqlDoc.cls
/// Jeff.liu@intersytems.com
/// 
/// Description
Class HCC.SVR.Prod.BS.SqlDoc Extends Ens.BusinessService
{

Property SqlDocRegXSL As %String(MAXLEN = "") [ InitialExpression = {..SetSqlDocXSL()} ];

Parameter SETTINGS = "SqlDocRegXSL:Basic:fileSelector";

/// Description
Method SqlDocRegService(MsgId As %String, message As %Stream.GlobalCharacter) As %Status
{
     Set sc = $$$OK
     Set tRequest = ##class(HCC.SVR.Prod.MSG.SVRRequest).%New()
     set tRequest.MsgId =MsgId
     set tRequest.Action = "DocumentRegister"
     set tRequest.DocType= "RCMR_IN000002UV02"
     set test = ##class(HS.Util.StreamUtils).EnsureString(message)
    set tResult=""
    set tsc = ##class(%XML.XSLT.CompiledStyleSheet).CreateFromFile(..SqlDocRegXSL,.xsl)
    set tsc=##class(%XML.XSLT.Transformer).TransformStreamWithCompiledXSL(message,xsl,.tResult)
   //set tsc=##class(%XML.XSLT.Transformer).TransformStringWithCompiledXSL(test,xsl,.tResult)
   set test=##class(HS.Util.StreamUtils).EnsureString(tResult)
   if $$$ISERR(tsc) Do $system.Status.DecomposeStatus(tsc,.err,"-d") For i=1:1:err $$$TRACE( err(i) )
   set test=$REPLACE(test,"<?xml version=""1.0"" encoding=""UTF-8""?>","")
   set tResult=##class(HS.Util.StreamUtils).EnsureStream(test)
   do ##class(HCC.Utils.Functions).toUTF8(.tResult)
   set tRequest.Message=tResult
    //$$$TRACE("xslRESSSSSSULT"_tRequest.Message)
    Set tStatus = ..SendRequestSync("HCC.SVR.Prod.BP.MainControlGeneral", tRequest,.tResponse)

     Return sc
}

ClassMethod SQLDocReg(pDocInfo As %String, pDoc As %Stream.GlobalCharacter) As %Status
{
    #Dim regModel as HCC.DocRepository.SqlDocRegModel
      Set tDocInfo = {}.%FromJSON(pDocInfo)

      set regModel=##class(HCC.DocRepository.SqlDocRegModel).%New()
        set tDocContent =""

         Do pDoc.Rewind()
        while 'pDoc.AtEnd
        {
        Set tDocContent = tDocContent_pDoc.Read()
        }
        
        set tDocContentBase64=$system.Encryption.Base64Encode($ZCONVERT(tDocContent,"O","UTF8"),1)
         /// 病人ID
         set regModel.PatNum=tDocInfo.PatientIDNo
        /// 门诊号标识
        Set regModel.OutPatNum =tDocInfo.OutPatientNum
        /// 住院号标识
        Set regModel.InPatNum =tDocInfo.InPatientNum
        /// 患者姓名
        Set regModel.PatName =tDocInfo.PatientName
        /// 文档类型代码
        Set regModel.DocType=tDocInfo.DocType
        /// 消息创建时间
       
       set tempT= $ZDATETIME($ZDATETIMEH($ZTIMESTAMP,-3),3,1,4)
         Set regModel.MsgCreateTime = ##class(Ens.Util.Time).ConvertDateTime(tempT,"%q(1)","%q(0)",,.tsc)
         /// 消息id
        set regModel.RegID =##class(%SYSTEM.Util).CreateGUID()
        /// 发送系统标识
        Set regModel.SenderID = "System"
        /// 文档流水号 文档唯一标识
        Set regModel.DocNum = tDocInfo.DocNum
        /// 文档类型代码
        Set regModel.DocType = tDocInfo.DocType
        /// 文档类型名称
        set regModel.DocName=tDocInfo.DocName
        /// 文档生成日期时间
        Set regModel.DocGenDate=##class(Ens.Util.Time).ConvertDateTime(tDocInfo.DocCreatTime,"%q(1)","%Y%m%d",1,.tsc)
        /// 文档版本号
        Set regModel.DocVersion = tDocInfo.DocVersion

        /// 文档提供的医院标识
        Set regModel.DocOriginHosCode =tDocInfo.HospitalID
        /// 文档提供的医院名称
        Set regModel.DocOriginHosName =tDocInfo.HospitalName
        /// 科室代码
        Set regModel.DeptCode =tDocInfo.DeptCode
        /// 文档创建者编号
        Set regModel.DocAuthorCode =tDocInfo.AuthorID
        /// 文档创建者姓名
        Set regModel.DocAuthorName =tDocInfo.AuthorName
        /// 文档保管的医院标识                    
        Set regModel.DocCustodianCode =tDocInfo.CustodianCode
        /// 文档保管的医院名称
        Set regModel.DocCustodianName =tDocInfo.CustodianName
        Set regModel.EncounterTimeStart =##class(Ens.Util.Time).ConvertDateTime(tDocInfo.EncBeginTime_".000","%q(1)","%Y%m%d%H%m%S",1,.tsc)
        //Set regModel.EncounterTimeStart =tDocInfo.EncBeginTime
        /// 患者就诊日期截至时间  
        Set regModel.EncounterTimeEnd =##class(Ens.Util.Time).ConvertDateTime(tDocInfo.EncEndTime_".000","%q(1)","%Y%m%d%H%m%S",1,.tsc)
        set regModel.DocCode64=tDocContentBase64

        set tsc= regModel.XMLExportToStream(.regXml)
        
       // zw tDocInfo 

       // w ##class(HS.Util.StreamUtils).EnsureString(regXml)

        do ##class(Ens.Director).CreateBusinessService("HCC.SVR.Prod.BS.SqlDoc",.tService)

        set tsc = tService.SqlDocRegService(regModel.RegID,regXml)

        return tsc
}

ClassMethod SetSqlDocXSL() As %String
{
    set installPath= ##class(%SYSTEM.Util).InstallDirectory()
    set wPath= installPath_"csp\xslt\HCC\xsl\DocRegister.xsl"
    set uPath= installPath_"csp/xslt/HCC/xsl/DocRegister.xsl"
    //2 for Windows, 3 for UNIX®, and 0 if not known.
    set os=$ZVERSION(1)
    return $CASE(os,2: wPath,3: uPath,0: "")
}

}
